package com.example.connecting_dots_android;


import model.Directions;
import model.GameHandler;
import model.GameMVC;
import model.HighScoresFileModel;
import model.IHighScoresModel;
import model.Line;
import model.Player;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import exceptions.InvalidLineIndexingException;
import exceptions.NoSuchDirectionException;

public class StartGame extends Activity {
	private IHighScoresModel highScoresModel;
	private GameMVC personalMVC;
	private Player playerOne, playerTwo;
	private int turns = -1;
	private int gameSize;
	private int row;
	private int col;
	Directions direction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		// Show the Up button in the action bar.
		setupActionBar();
		
		this.highScoresModel = new HighScoresFileModel();
		this.personalMVC = new GameMVC();
		playerOne = new Player("Pe6o", "x");
		playerTwo = new Player("Go6o", "y");
		this.personalMVC.setHandler(new GameHandler(4));
		TextView firstPlayer = (TextView) findViewById(R.id.idNameFirstPlayerLabel);
		TextView secondPlayer = (TextView) findViewById(R.id.idNameSecondPlayerLabel);
		firstPlayer.setText(playerOne.getName());
		secondPlayer.setText(playerTwo.getName());
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.start_game, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void show(View view) {
		Player currentPlayer = playerOnTurn();
		
		
		//ne sym go izmislila o6te
//		if (isOver()) {
//			SquareLayout sl = (SquareLayout) findViewById(R.id.idSquareLayout);
//			sl.setEnabled(false);
//			TextView winner = (TextView) findViewById(R.id.winner);
//			winner.setVisibility(1);
//			winner.setText("Winner is "+currentPlayer.getName()+" with score of "+currentPlayer.getCurrentScore());
//		}
		
		boolean scored;
		int emptyCellsInStartOfTurn = personalMVC.getHandler()
				.getEmptyCellsCount();
		
		//set current line row col and direction
		setLineCoordinates(view);

		try {
			
			Line line = new Line(row, col, direction);
			scored = personalMVC.getHandler().updateGameBoard(line,
					currentPlayer.getSign());
			view.setBackgroundColor(Color.BLUE);
			
			if (scored) {
//				Log.i("last silled cell row and col", ""
//						+ personalMVC.getHandler().getLastFilledSignCellRow() + " "
//						+ personalMVC.getHandler().getLastFilledSignCellCol());// neeeee
				int numberOfPointsScored = emptyCellsInStartOfTurn
						- this.personalMVC.getHandler().getEmptyCellsCount();

				
				//increse score of the current player
				increasePlayerScore(currentPlayer, numberOfPointsScored);
				
//				if(numberOfPointsScored==1){
				TextView setSign = getTextViewToWritePLayerSign( personalMVC.getHandler().getLastFilledSignCellRow(),
						+ personalMVC.getHandler().getLastFilledSignCellCol());
				setSign.setText(currentPlayer.getSign());
				
//				}else{
//					if()
//				}
				
				turns++;
//				setSign.setText(currentPlayer.getSign());
				view.setEnabled(false);
			}
		} catch (InvalidLineIndexingException e) {

			e.printStackTrace();
		} catch (NoSuchDirectionException e) {

			e.printStackTrace();
		}
//		for (int i = 0; i < personalMVC.getHandler().getBoardSize(); i++) {
//			String result = "";
//			for (int j = 0; j < personalMVC.getHandler().getBoardSize(); j++) {
//				result += personalMVC.getHandler().getStringForRowAndCol(i, j);
//			}
//			Log.i("red: ", result);
//		}

	}

	private void setLineCoordinates(View view) {
		setRow((Button) view);
		setCol((Button) view);
		setDirection((Button) view);
	}

	private void increasePlayerScore(Player currentPlayer,
			int numberOfPointsScored) {
		if (currentPlayer!= null && currentPlayer.equals(playerOne)) {
			playerOne.increaseScore(numberOfPointsScored);
//			Log.i("player one score is ",
//					"" + playerOne.getCurrentScore());
			((TextView) findViewById(R.id.idNameFirstPlayerLabel))
					.setText("" + playerOne.getName() + ": "
							+ playerOne.getCurrentScore());
		} else {
			playerTwo.increaseScore(numberOfPointsScored);
			((TextView) findViewById(R.id.idNameSecondPlayerLabel))
					.setText("" + playerTwo.getName() + ": "
							+ playerTwo.getCurrentScore());

		}
	}

	private Player playerOnTurn() {

		this.turns++;
		if (turns % 2 != 0) {
			return playerOne;
		} else {
			return playerTwo;
		}

	}

	private boolean isOver() {

		if (personalMVC.getHandler().getEmptyCellsCount() == 0)
			return true;
		return false;

	}

	public void setRow(Button btn) {
		int btnID = btn.getId();

		switch (btnID) {
		case R.id.btn02:
		case R.id.btn04:
		case R.id.btn06:
		case R.id.btn08:
		case R.id.btn11:
		case R.id.btn13:
		case R.id.btn15:
		case R.id.btn17:
		case R.id.btn19:

			row = 0;
			break;

		case R.id.btn22:
		case R.id.btn24:
		case R.id.btn26:
		case R.id.btn28:
		case R.id.btn31:
		case R.id.btn33:
		case R.id.btn35:
		case R.id.btn37:
		case R.id.btn39:

			row = 1;
			break;

		case R.id.btn42:
		case R.id.btn44:
		case R.id.btn46:
		case R.id.btn48:
		case R.id.btn51:
		case R.id.btn53:
		case R.id.btn55:
		case R.id.btn57:
		case R.id.btn59:

			row = 2;
			break;

		case R.id.btn62:
		case R.id.btn64:
		case R.id.btn66:
		case R.id.btn68:
		case R.id.btn71:
		case R.id.btn73:
		case R.id.btn75:
		case R.id.btn77:
		case R.id.btn79:

			row = 3;
			break;

		case R.id.btn82:
		case R.id.btn84:
		case R.id.btn86:
		case R.id.btn88:

			row = 4;
			break;

		}
	}

	public void setCol(Button btn) {
		int btnID = btn.getId();

		switch (btnID) {
		case R.id.btn02:
		case R.id.btn11:
		case R.id.btn22:
		case R.id.btn31:
		case R.id.btn42:
		case R.id.btn51:
		case R.id.btn62:
		case R.id.btn71:
		case R.id.btn82:

			col = 0;
			break;

		case R.id.btn04:
		case R.id.btn13:
		case R.id.btn24:
		case R.id.btn33:
		case R.id.btn44:
		case R.id.btn53:
		case R.id.btn64:
		case R.id.btn73:
		case R.id.btn84:

			col = 1;
			break;

		case R.id.btn06:
		case R.id.btn15:
		case R.id.btn26:
		case R.id.btn35:
		case R.id.btn46:
		case R.id.btn55:
		case R.id.btn66:
		case R.id.btn75:
		case R.id.btn86:

			col = 2;
			break;

		case R.id.btn08:
		case R.id.btn17:
		case R.id.btn28:
		case R.id.btn37:
		case R.id.btn48:
		case R.id.btn57:
		case R.id.btn68:
		case R.id.btn77:
		case R.id.btn88:

			col = 3;
			break;

		case R.id.btn19:
		case R.id.btn39:
		case R.id.btn59:
		case R.id.btn79:

			col = 4;
			break;

		}
	}

	public void setDirection(Button btn) {
		int btnID = btn.getId();

		switch (btnID) {
		case R.id.btn02:
		case R.id.btn04:
		case R.id.btn06:
		case R.id.btn08:
		case R.id.btn22:
		case R.id.btn24:
		case R.id.btn26:
		case R.id.btn28:
		case R.id.btn42:
		case R.id.btn44:
		case R.id.btn46:
		case R.id.btn48:
		case R.id.btn62:
		case R.id.btn64:
		case R.id.btn66:
		case R.id.btn68:
		case R.id.btn82:
		case R.id.btn84:
		case R.id.btn86:
		case R.id.btn88:

			direction = Directions.R;
			break;

		case R.id.btn11:
		case R.id.btn13:
		case R.id.btn15:
		case R.id.btn17:
		case R.id.btn19:
		case R.id.btn31:
		case R.id.btn33:
		case R.id.btn35:
		case R.id.btn37:
		case R.id.btn39:
		case R.id.btn51:
		case R.id.btn53:
		case R.id.btn55:
		case R.id.btn57:
		case R.id.btn59:
		case R.id.btn71:
		case R.id.btn73:
		case R.id.btn75:
		case R.id.btn77:
		case R.id.btn79:

			direction = Directions.D;
			break;

		}
	}

	public TextView getTextViewToWritePLayerSign(int row, int col) {
		int useToCalculateTextViewID = row * 10 + col + 1;

		switch (useToCalculateTextViewID) {
		case 12:
			return (TextView) findViewById(R.id.sign12);
		case 14:
			return (TextView) findViewById(R.id.sign14);
		case 16:
			return (TextView) findViewById(R.id.sign16);
		case 18:
			return (TextView) findViewById(R.id.sign18);
		case 32:
			return (TextView) findViewById(R.id.sign32);
		case 34:
			return (TextView) findViewById(R.id.sign34);
		case 36:
			return (TextView) findViewById(R.id.sign36);
		case 38:
			return (TextView) findViewById(R.id.sign38);
		case 52:
			return (TextView) findViewById(R.id.sign52);
		case 54:
			return (TextView) findViewById(R.id.sign54);
		case 56:
			return (TextView) findViewById(R.id.sign56);
		case 58:
			return (TextView) findViewById(R.id.sign58);
		case 72:
			return (TextView) findViewById(R.id.sign72);
		case 74:
			return (TextView) findViewById(R.id.sign74);
		case 76:
			return (TextView) findViewById(R.id.sign76);
		case 78:
			return (TextView) findViewById(R.id.sign78);
		default:
			return null;

		}
	}

}
